package cn.edu.scau.cmi.yewangjiang.client;
import cn.edu.scau.cmi.yewangjiang.composite.consistent.*;
import java.util.*;
import javafx.application.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.*;
import javafx.stage.*;
import javafx.event.*;
import javafx.geometry.Pos;

public class ConsistentCompositeClient extends Application{ 

	private static Set<String> nameSet;
	private static List<Organization> objectList;
	public static void main(String[] args) {
	    nameSet=new HashSet<String>();
	    objectList=new ArrayList<Organization>();
	    launch(args); // 启动JavaFX应用，接下来会跳到start方法
	}
	@Override
    public void start(Stage stage) throws Exception {
    	stage.setTitle("透明形式的组合模式");  
    	Pane pane=new Pane();
        GUI(pane); 
        Scene scene = new Scene(pane, 800, 800); 
        stage.setScene(scene); // 设置舞台的场景
        stage.setResizable(false); // 设置舞台的尺寸是否允许变化
        stage.show(); // 显示舞台。相当于JFrame的setVisible(true)      
    }
	
    public void GUI(Pane pane) {
    	Label label1=new Label("请输入目标对象的名字："); 
    	label1.setLayoutX(10);
     	label1.setLayoutY(10); 
     	label1.setPrefSize(220, 30); 
     	label1.setTextFill(Color.RED); 
     	pane.getChildren().add(label1);
     	
     	Label label2=new Label("请输入创建对象的位置：\n(此处为空默认在根节点)"); 
    	label2.setLayoutX(10);
    	label2.setLayoutY(80); 
    	label2.setTextFill(Color.RED); 
    	label2.setWrapText(false);
    	pane.getChildren().add(label2);
    	
    	Label label3=new Label("是否显示所创建对象的树状图?");
    	label3.setLayoutX(10); 
    	label3.setLayoutY(200);   
    	label3.setPrefHeight(30); 
    	label3.setTextFill(Color.RED); 
    	pane.getChildren().add(label3); 
    	
    	Label label4=new Label();
    	label4.setLayoutX(540); 
    	label4.setLayoutY(80);  
    	label4.setPrefHeight(30); 
    	label4.setTextFill(Color.RED); 
    	pane.getChildren().add(label4);
    	
    	Label label5=new Label();
    	label5.setLayoutX(540); 
    	label5.setLayoutY(10);  
    	label5.setPrefHeight(30);  
    	label5.setTextFill(Color.RED); 
    	pane.getChildren().add(label5);
     	
     	TextField field1=new TextField(); 
	    field1.setLayoutX(220);
	    field1.setLayoutY(10); 
	    field1.setPrefSize(300,40);
	    pane.getChildren().add(field1);  
	    
	    TextField field2=new TextField(); 
	    field2.setLayoutX(220);
	    field2.setLayoutY(80); 
	    field2.setPrefSize(300,40); 
	    pane.getChildren().add(field2);  
	    
	    TextArea textarea=new TextArea();
	    textarea.setLayoutX(10);
	    textarea.setLayoutY(250); 
	    textarea.setPrefSize(790,550); 
	    pane.getChildren().add(textarea); 
	    
	    Button button1=new Button("添加组合件");  
    	button1.setLayoutX(170); 
    	button1.setLayoutY(140); 
    	button1.setPrefSize(100, 20);
    	button1.setOnAction(new EventHandler<ActionEvent>() { // 设置按钮的单击事件
    	    @Override
    	    public void handle(ActionEvent arg0) { // 处理单击事件   
    	       String parentString=field2.getText();
    	       String childString=field1.getText();
    	       if(childString.length()==0) {
    	    	   label5.setText("请输入信息！");
    	    	   return ; 
    	       }
    	       if(nameSet.contains(childString)) {
    	    	   label5.setText("姓名重复，请重新输入！");
    	    	   return ;
    	       }
    	       if(parentString.length()==0) {
    	    	   Team t=new Team(childString);
    	    	   t.setParent(null);
    	    	   objectList.add(t);
    	    	   nameSet.add(childString);
    	    	   label5.setText("添加成功！");
    	    	   label4.setText("加入结点成功");
  	    	       return ;
    	       }
    	       int num=objectList.size();
    	       for(int i=0; i<num; i++) {
    	    	   Organization org=(Organization)objectList.get(i);
    	    	   if(org instanceof Team) {
    	    		   Team t=(Team)org;
    	    		   if(t.getName().equals(parentString)) {
    	    			   List<Organization> list=t.getChildren();
    	    			   Team team=new Team(childString);
    	    			   team.setParent(t);
    	    			   list.add(team); 
    	    			   objectList.add(team);
    	    			   nameSet.add(childString);
    	    			   label5.setText("添加成功！"); 
    	    			   label4.setText("加入结点成功"); 
    	    			   return ;
    	    		   }
    	    	   }
    	       }
    	       label5.setText("");
    	       label4.setText("输入的位置错误！"); 
    	} 
    	});
    	pane.getChildren().add(button1); 
    	
    	Button button2=new Button("添加叶结点");    
    	button2.setLayoutX(290); 
    	button2.setLayoutY(140);
    	button2.setPrefSize(100, 20);
    	button2.setOnAction(new EventHandler<ActionEvent>() { // 设置按钮的单击事件
    	    @Override
    	    public void handle(ActionEvent arg0) { // 处理单击事件   
    	       String parentString=field2.getText();
     	       String childString=field1.getText();
     	       if(childString.length()==0) {
     	    	   label5.setText("请输入信息！");
     	    	   return ; 
     	       }
     	       if(nameSet.contains(childString)) {
     	    	   label5.setText("姓名重复，请重新输入！");
     	    	   return ;
     	       }
     	       if(parentString.length()==0) {
     	    	   Person p=new Person(childString);
     	    	   p.setParent(null);
     	    	   objectList.add(p);
     	    	   nameSet.add(childString);
     	    	   label5.setText("添加成功！");
     	    	   label4.setText("加入结点成功");
     	    	  field1.setText(""); 
       	          field2.setText("");
   	    	       return ;
     	       }
     	       int num=objectList.size(); 
     	       for(int i=0; i<num; i++) {
     	    	   Organization org=(Organization)objectList.get(i);
     	    	   if(org instanceof Team) {
     	    		   Team t=(Team)org;
     	    		   if(t.getName().equals(parentString)) {
     	    			   List<Organization> list=t.getChildren();
     	    			   Person person=new Person(childString);
     	    			   person.setParent(t);
     	    			   list.add(person);
     	    			   objectList.add(person);
     	    			   nameSet.add(childString);
     	    			   label5.setText("添加成功！"); 
     	    			   label4.setText("加入结点成功");
     	    			  field1.setText("");
     	      	          field2.setText("");
     	    			   return ;
     	    		   }
     	    	   }
     	       }
     	       label5.setText("");
     	       label4.setText("输入的位置错误！"); 
    	} 
    	});
    	pane.getChildren().add(button2); 
    	
    	Button button3=new Button("删除该对象");  
    	button3.setLayoutX(410); 
    	button3.setLayoutY(140);
    	button3.setPrefSize(100, 20);
    	button3.setOnAction(new EventHandler<ActionEvent>() { // 设置按钮的单击事件
    	    @Override
    	    public void handle(ActionEvent arg0) { // 处理单击事件   
      	        String childString=field1.getText();
      	      if(childString.length()==0) {
    	    	   label5.setText("请输入信息！");
    	    	   return ; 
    	       }
    	       if(nameSet.contains(childString)==false) {
    	    	   label5.setText("姓名不存在，请重新输入！");
    	    	   return ;
    	       }
    	       for(int i=0; i<objectList.size(); i++) {
    	    	  Organization org=(Organization)objectList.get(i);
    	    	  if(org instanceof Team) {
    	    		  Team t=(Team)org;
    	    		   if(t.getName().equals(childString)) {
    	    			   Team parent=(Team)t.getParent();
    	    			   if(parent!=null) {
    	    				   List<Organization> list=parent.getChildren();
    	    				   list.remove(t);  
    	    			   }
    	    			   objectList.removeAll(t.getChildren());
                           objectList.remove(t);
                           break;
    	    		   }
    	    	  }
    	    	  if(org instanceof Person) {
    	    		  Person p=(Person)org;
    	    		  if(p.getName().equals(childString)) {
    	    			  Team parent=(Team)p.getParent();
    	    			  if(parent!=null) {
    	    				  List<Organization> list=parent.getChildren();
   	    				      list.remove(p); 
    	    			  }
    	    			  objectList.remove(p);
	    				  break;
    	    		  }
    	    	  }
    	       }
    	       for(int i=0; i<objectList.size(); i++) {
    	    	   Organization org=objectList.get(i);
    	    	   if(org instanceof Team) {
    	    		   Team t=(Team)org;
    	    		   if(nameSet.contains(t.getName())==false) {
    	    			   nameSet.remove(t.getName());
    	    		   }
    	    	   }
    	    	   if(org instanceof Person) {
    	    		   Person t=(Person)org;
    	    		   if(nameSet.contains(t.getName())==false) {
    	    			   nameSet.remove(t.getName());
    	    		   } 
    	    	   }
    	       }
    	       label5.setText("删除成功！"); 
 			   label4.setText("");
 			    field1.setText("");
   	            field2.setText("");
    	} 
    	}); 
    	pane.getChildren().add(button3); 
    	
    	Button button4=new Button("显示");  
    	button4.setLayoutX(250); 
    	button4.setLayoutY(200);
    	button4.setPrefSize(100, 20);
    	button4.setOnAction(new EventHandler<ActionEvent>() { // 设置按钮的单击事件
    	    @Override
    	    public void handle(ActionEvent arg0) { // 处理单击事件 
    	    	textarea.clear(); 
    	       for(int i=0; i<objectList.size(); i++) {
    	    	   Organization org=objectList.get(i);
    	    	   if(org instanceof Person) {
    	       		Person p=(Person)org;
    	       		if(p.getParent()==null)
    	       			show(textarea,p);
    	       	}
    	    	else {
    	       	Team t=(Team)org;
    	       	if(t.getParent()==null)
    	       		show(textarea,t);
    	       }
    	     }
    	}  
    	}); 
    	pane.getChildren().add(button4);  
    }
    void show(TextArea textarea,Organization org) {
    	if(org instanceof Person) { 
    		Person p=(Person)org;
    		textarea.appendText("-->"+p.getName()+"\n");
    		return ;
    	} 
    	Team t=(Team)org;
    	List<Organization> childList=t.getChildren();
    	if(childList.size()==0) {
    		textarea.appendText("-->"+t.getName()+"\n");
    		return ;
    	}
      for(int i=0; i<childList.size(); i++) {
    		textarea.appendText("-->"+t.getName());
    		show(textarea,childList.get(i));
    	}
    }
}
